package com.sita.tingterest.fragment;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v4.app.Fragment;

import com.sita.tingterest.services.BoundServices;
import com.sita.tingterest.services.PlayService;
import com.sita.tingterest.utils.L;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment {

    protected PlayService mPlayService;
    protected BoundServices mBoundService;

    private ServiceConnection mPlayServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mPlayService = null;
            //  mBoundService= null;
            L.l("MY TAG", " onServiceDisconnected BaseFragment); ");
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //    BoundServices.MyBinder myBinder = (BoundServices.MyBinder) service;
            //    mBoundService = myBinder.getServices();
            mPlayService = ((PlayService.PlayBinder) service).getService();
            mPlayService.setOnMusicEventListener(mMusicEventListener);
            onChange(mPlayService.getPlayingPosition());
            L.l("MY TAG", " onServiceConnected( BaseFragment); ");
        }
    };
    private PlayService.OnMusicEventListener mMusicEventListener =
            new PlayService.OnMusicEventListener() {
                @Override
                public void onPublish(int progress) {
                    L.l("MY TAG", " onPublish(int progress)  BaseFragment); ");
                    BaseFragment.this.onPublish(progress);
                }

                @Override
                public void onChange(int position) {
                    L.l("MY TAG", " onChange(int position)  BaseFragment); ");
                    BaseFragment.this.onChange(position);
                }
            };

    public BaseFragment() {
        //  allowBindService();
        // Required empty public constructor
    }

    /**
     * Fragment的view加载完成后回调
     */
    public void allowBindService() {
        L.l("MY TAG", " allowBindService()  BaseFragment); ");
        getActivity().bindService(new Intent(getActivity(), PlayService.class), mPlayServiceConnection,
                Context.BIND_AUTO_CREATE);
       /* bindService(new Intent(getActivity(), PlayService.class), mPlayServiceConnection,
                Context.BIND_AUTO_CREATE);*/
    }

    //  protected abstract void bindService(Intent intent, ServiceConnection mPlayServiceConnection, int bindAutoCreate);

    /**
     * fragment的view消失后回调
     */
    public void allowUnbindService() {
        L.l("MY TAG", " allowUnbindService()  BaseFragment); ");
        getActivity().unbindService(mPlayServiceConnection);
        //  unbindService(mPlayServiceConnection);
    }

    //  protected abstract void unbindService(ServiceConnection                                                   mPlayServiceConnection);

    public abstract void onPublish(int progress);

    // public  void onPublish(int progress);

    /**
     * 切换歌曲
     *
     * @param position 歌曲在list中的位置
     */
    public abstract void onChange(int position);

  /*  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_base, container, false);
    }*/


}
